package it.apiconsulting.live.view;

import it.apiconsulting.live.controller.ClienteManager;
import it.apiconsulting.live.model.Cliente;

import java.util.List;


public class ClienteAction extends BaseAction {

	private static final long serialVersionUID = 9149826260758390091L;
	private Cliente cliente;
	private List<Cliente> clienti;
	private String id;
	
	private ClienteManager linkClienteController;

	public ClienteAction() {
		linkClienteController = new ClienteManager();
		function = "Cliente";
	}

	public String execute() {
		this.clienti = linkClienteController.list();
		return SUCCESS;
	}

	public String add() {
		
		System.out.println(getCliente());
		try {
			if ( getId() != null && !getId().equals("") ){
				if ( linkClienteController.getClienteById(Long.parseLong(getId())) != null ) {
					getCliente().setId(Long.parseLong(getId()));
					linkClienteController.update(getCliente());
				}
			}  else linkClienteController.add(getCliente());
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.clienti = linkClienteController.list();	
		return SUCCESS;
	}

	public String delete() {
		linkClienteController.delete(Long.parseLong(getId()));
		return SUCCESS;
	}
	
	public String edit() {
		setCliente(linkClienteController.getClienteById(Long.parseLong(getId())));
		return SUCCESS;
	}

	public Cliente getCliente() {
		return cliente;
	}

	public List<Cliente> getClienti() {
		return clienti;
	}

	public void setCliente(Cliente cliente) {
		this.cliente = cliente;
	}

	public void setClienti(List<Cliente> clienti) {
		this.clienti = clienti;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getFunction() {
		return function;
	}

	public void setFunction(String function) {
		this.function = function;
	}
}
